.TH swap(std::move_only_function) 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
swap(std::move_only_function) \- swap(std::move_only_function)

.SH Synopsis
   friend void swap( move_only_function& lhs, move_only_function& rhs )   (since C++23)
   noexcept;

   Overloads the std::swap algorithm for std::move_only_function. Exchanges the state
   of lhs with that of rhs. Effectively calls lhs.swap(rhs).

   This function is not visible to ordinary unqualified or qualified lookup, and can
   only be found by argument-dependent lookup when
   std::move_only_function<FunctionType> is an associated class of the arguments.

.SH Parameters

   lhs, rhs - std::move_only_function objects whose states to swap

.SH Return value

   \fI(none)\fP

.SH Example


// Run this code

 #include <concepts>
 #include <functional>
 #include <iostream>

 void foo(const char* str, int x)
 {
     std::cout << "foo(\\"" << str << "\\", " << x << ")\\n";
 }

 void bar(const char* str, int x)
 {
     std::cout << "bar(\\"" << str << "\\", " << x << ")\\n";
 }

 int main()
 {
     std::move_only_function<void(const char*, int) const> f1{foo};
     std::move_only_function<void(const char*, int) const> f2{bar};

     f1("f1", 1);
     f2("f2", 2);

     std::cout << "std::ranges::swap(f1, f2);\\n";
     std::ranges::swap(f1, f2); // finds the hidden friend

     f1("f1", 1);
     f2("f2", 2);
 }

.SH Output:

 foo("f1", 1)
 bar("f2", 2)
 std::ranges::swap(f1, f2);
 bar("f1", 1)
 foo("f2", 2)

.SH See also

   swap                     swaps the targets of two std::move_only_function objects
   (C++23)                  \fI(public member function)\fP
   std::swap(std::function) specializes the std::swap algorithm
   \fI(C++11)\fP                  \fI(function template)\fP
